//
//  EMMDropDownView.m
//  EMMApp
//
//  Created by Mac on 16/5/4.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "EMMDropDownView.h"
#define ITEMVIEW_HEIGHT 44.f
#import "EMMDropDownCell.h"
@interface EMMDropDownView () {
    int i;
    NSInteger current;
}

@property (nonatomic, copy) NSArray *items;
@property (nonatomic, strong) UIView *menuView;
@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) UIImageView *selectImg;
@property (nonatomic, copy) NSArray *numArr;

@end

@implementation EMMDropDownView

- (id)initWithFrame:(CGRect)frame Items:(NSArray *)items
{
    self = [self init];
    if (self) {
        self.frame = frame;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3f];
        _items = items;
        [self drawView];
    }
    return self;
}

- (void)drawView {
    _menuView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 300)];
    _menuView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_menuView];
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 300) style:UITableViewStylePlain];
    _table.delegate = self;
    _table.dataSource = self;
    [_menuView addSubview:_table];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _items.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ITEMVIEW_HEIGHT;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellIdentifier = @"EMMDropDownCell";
    EMMDropDownCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"EMMDropDownCell" owner:self options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, _menuView.frame.size.width-50, 20)];
    title.text = _items[indexPath.row];
    title.textColor = [UIColor darkGrayColor];
    title.font = [UIFont systemFontOfSize:15];
    [cell addSubview:title];
    
    _selectImg = [[UIImageView alloc]initWithFrame:CGRectMake(_menuView.frame.size.width - 30, 15, 19, 15)];
    _selectImg.image = [UIImage imageNamed:@"pullmenu_select.png"];
    if (indexPath.row == 0) {
        _selectImg.hidden = NO;
    }else {
       _selectImg.hidden = YES;
    }
    
    [cell addSubview:_selectImg];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_itemSelected) {
        _itemSelected(indexPath.row);
    }
    current=indexPath.row;
    [_table reloadData];
    [self close];
}
- (UITableViewCellAccessoryType)tableView:(UITableView*)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath*)indexPath {
    if(current==indexPath.row) {
        _selectImg.hidden = NO;
        return UITableViewCellAccessoryNone;
    } else {
        _selectImg.hidden = YES;
        return UITableViewCellAccessoryNone;
    }
}

- (void)open:(UIView *)views {
    BOOL isHave = NO;
    for (id obj in [views subviews]) {
        if ([obj isKindOfClass:[EMMDropDownView class]] ) {
            isHave = YES;
            break;
        }
    }
    if (isHave == NO) {
        [views addSubview:self];
    }
    _menuView.transform = CGAffineTransformMakeTranslation(0.f, -ITEMVIEW_HEIGHT*_items.count);
    self.alpha = 0;
    //    _menuView.alpha = 0;
    
    self.isOpen = YES;
    [UIView animateWithDuration:.7f delay:0.f usingSpringWithDamping:.7f initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        _menuView.transform = CGAffineTransformMakeTranslation(0.f, 0);
        //        _menuView.alpha = 1.0;
        self.alpha = 1.0;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3f];
        
    } completion:nil];
}

- (void)close {
    self.isOpen = NO;
    [UIView animateWithDuration:0.3f animations:^{
        _menuView.transform = CGAffineTransformMakeTranslation(0.f, -ITEMVIEW_HEIGHT*_items.count);
        //        _menuView.alpha = 0;
        //        self.alpha = 0;
        self.backgroundColor = [UIColor clearColor];
    } completion:^(BOOL finish){
        self.alpha = 0;
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self close];
}

@end
